<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-select v-model="dataForm.key" clearable placeholder="审批状态">
          <el-option :key="0" label="审批中" :value="0"></el-option>
          <el-option :key="1" label="审批通过" :value="1"></el-option>
          <el-option :key="2" label="驳回审批" :value="2"></el-option>
        </el-select>
        <!-- <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input> -->
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button type="primary" @click="addOrUpdateHandle()">发起请假</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        prop="restName"
        header-align="center"
        align="center"
        label="请假人">
      </el-table-column>
      <el-table-column
        prop="auditPeopleName"
        header-align="center"
        align="center"
        label="审批人">
      </el-table-column>
      <el-table-column
        prop="auditStatus"
        header-align="center"
        width="80"
        align="center"
        label="审批状态">
        <template slot-scope="scope">
          <el-tag
            :key="scope.row.auditStatus"
            :type="scope.row.auditStatus == 0 ? 'danger' : scope.row.auditStatus == 1 ? 'success' : 'info'"
            effect="plain"
            size="small">{{scope.row.auditStatus == 0 ? '审核中' : scope.row.auditStatus == 1 ? '审批通过' : scope.row.auditStatus == 2 ? '审批驳回':'审批结束'}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="reason"
        header-align="center"
        align="center"
        width="200px"
        label="请假原因">
      </el-table-column>
      <el-table-column
        prop="startTime"
        header-align="center"
        align="center"
        width="200px"
        label="请假开始时间">
      </el-table-column>
      <el-table-column
        prop="endTime"
        header-align="center"
        align="center"
        width="200px"
        label="请假结束时间">
      </el-table-column>
      <el-table-column
        prop="auditTime"
        header-align="center"
        align="center"
        width="200px"
        label="审批时间">
      </el-table-column>
      <el-table-column
        prop="auditLevel"
        header-align="center"
        width="100"
        align="center"
        label="审核阶段">
        <template slot-scope="scope">
          <el-tag
            type=""
            size="small">{{scope.row.auditLevel == 1 ? '班主任审核' : '书记审核'}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="restStatus"
        header-align="center"
        width="80"
        align="center"
        label="请假状态">
        <template slot-scope="scope">
          <el-tag
            :key="scope.row.auditStatus"
            :type="scope.row.auditStatus == 3 ? 'info' : 'success'"
            effect="plain"
            size="small">{{scope.row.auditStatus == 3 ? '学生撤销' : '正常'}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.auditStatus == 0" type="text" size="small" @click="passHandle(scope.row.auditId)">通过</el-button>
          <el-button v-if="scope.row.auditStatus == 0" type="text" size="small" @click="rejectHandle(scope.row.auditId)">驳回</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/master/audit/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'auditStatus': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 通过审批
      passHandle (id) {
        this.$confirm(`您确定通过该请假吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/master/audit/pass'),
            method: 'post',
            data: this.$http.adornData(id, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      //驳回审批
      rejectHandle (id) {
        this.$confirm(`您确定驳回该请假吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/master/audit/reject'),
            method: 'post',
            data: this.$http.adornData(id, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
